package com.fr.lintcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;

/**
*作者：furong
*日期：2017年1月16日
*时间：下午1:39:59
*/
public class Q98 {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {
        List<ListNode> list = new ArrayList<>();
        ListNode c = head;
        while (c != null) {
            list.add(c);
            c = c.next;
        }
        Collections.sort(list, new c());
        list.add(null);
        for (int i = 0; i < list.size() - 1; i++) {
            list.get(i).next = list.get(i + 1);
        }
        return list.get(0);
    }

    class c implements Comparator<ListNode> {

        @Override
        public int compare(ListNode o1, ListNode o2) {
            return o1.val - o2.val;
        }

    }

    @Test
    public void testA() {
        ListNode head = new ListNode(1);
        ListNode l1 = new ListNode(9);
        ListNode l2 = new ListNode(7);
        ListNode l3 = new ListNode(4);
        ListNode l4 = new ListNode(5);
        ListNode l5 = new ListNode(3);
        ListNode l6 = new ListNode(2);
        head.next = l1;
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        System.out.println(sortList(null));
    }
}
